//
//  JkmTitleMenuController.swift
//  SinaWb
//
//  Created by Jiang on 5/16/16.
//  Copyright © 2016 Jiang. All rights reserved.
//

import UIKit
/**
    在View之上添加了**ImageView**和**TableView**
 
    ### 弹出菜单的控制器 ###
    1. 在TableViewDataSouce中设置弹出列表显示的信息
    2. 在TableiViewDelegate中监听被选中的按钮
    3. 菜单的弹出后并不让后面的控制器消失,需要设置ModalPresentStyle为**Custom**
 
 */
class JkmTitleMenuController: UIViewController {
   
    private  var BackGroundImage : UIImageView = {
        let image = UIImageView(image:UIImage(named: "popover_background"))
        return image
    }()
    
   private var tableview : UITableView = {
        let view = UITableView()
        view.backgroundColor = UIColor.clearColor()
        
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableview.dataSource = self
        tableview.delegate = self
        tableview.separatorStyle = .None
        
        
        view.addSubview(BackGroundImage)
        view.addSubview(tableview)
        
        addConstains()
    }
    
    /**
            #### 添加子视图约束 ####
        1. BackGoundImage适配整个控制器View的大小
        2. TableView跟随BackGoundImage的大小
     
     */
   private func addConstains(){
        ///约束要在有父视图后才能使用
        BackGroundImage.snp_makeConstraints { (make) in
            make.bottom.equalTo(view.snp_bottom)
            make.top.equalTo(view.snp_top)
            make.right.equalTo(view.snp_right)
            make.left.equalTo(view.snp_left)
        }
    
        tableview.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(BackGroundImage.snp_centerX)
            make.centerY.equalTo(BackGroundImage.snp_centerY)
            make.width.equalTo(BackGroundImage.snp_width).offset(-15)
            make.height.equalTo(BackGroundImage.snp_height).offset(-15)
            
        }

    }
    

}

// MARK: - UITableViewDataSource
extension JkmTitleMenuController :UITableViewDataSource{
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("cell")
        if cell == nil {
            cell = UITableViewCell.init(style: .Default, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = "关注的人"

        cell?.textLabel?.textColor = UIColor.lightGrayColor()
        cell?.textLabel?.highlightedTextColor = UIColor.whiteColor()

        cell?.selectedBackgroundView = {
            let  view = UIView(frame: (cell?.frame)!)
            view.backgroundColor = UIColor.clearColor()
            return view
        }()
        
        cell?.backgroundColor = UIColor.clearColor()
        
        
        return cell!
    }
}

// MARK: - UITableViewDelegate
extension JkmTitleMenuController: UITableViewDelegate{
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
       
        let cell =  tableView.cellForRowAtIndexPath(indexPath)
        cell?.setHighlighted(true, animated: true)
        JkLog(indexPath.row)
    }
    
    
}